<template>
  <div class="app-container order-list-index">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      size="medium"
      class="ry_form"
    >
      <el-form-item v-if="YT_roles" :label="$t('customer.name')" prop="customerId">
        <el-select v-model="queryParams.customerId" filterable="filterable" remote="remote" clearable="clearable">
          <el-option v-for="item in wmsCustomerList" :key="item.customerName" :label="item.customerName" :value="item.id" />
        </el-select>
        </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t('common.search') }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{ $t('common.reset') }}</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane :label="$t('fiance.receivable')" name="1"></el-tab-pane>
      <el-tab-pane :label="$t('fiance.copeWith')" name="2"></el-tab-pane>
    </el-tabs>
    <!-- 列表 -->
    <TableComponents
      ref="tableRef"
      :url="'/wms/bmsFinance/taskList'"
      :columns="columnsAry"
      :params="{ status: 'INIT', ...queryParams, taskType: activeName }"
      :req="{
        page: 0,
        size: 10,
      }"
      :methodType="'paramsPost'"
      :return-data="returnData"
    >
      <el-table-column
        slot="operation"
        :label="$t('common.operation')"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="add(scope.$index, scope.row)"
          >
            {{ $t('common.add') }}
          </el-button>
        </template>
      </el-table-column>
    </TableComponents>
    <el-dialog
      :title="$t('common.operation')"
      :visible.sync="drawer"
      :size="'calc(100vw - 234px);'"
    >
      <el-form
        ref="ruleForm"
        :rules="rules"
        :model="ruleForm"
        label-width="80px"
        label-position="left"
        label-suffix=":"
      >
        <el-row class="content">
          <el-col
            v-for="(col, cindex) in editColumns"
            :key="cindex"
            :xs="24"
            :sm="col.col"
            :md="col.col"
            :lg="col.col"
            :xl="col.col"
          >
            <FormComponents :item-obj="col" :form="ruleForm" :isedit="true" />
          </el-col>
        </el-row>
        <span class="dialog-footer">
          <el-button @click="drawer = false">{{
            $t("common.cancel")
          }}</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">{{
            $t("common.confirm")
          }}</el-button>
        </span>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import dayjs from "dayjs";
import { updateBmsFinance } from "@/api/money";
import FormComponents from "@/components/FormComponents/index.vue";
import TableComponents from "@/components/Table/list.vue";
import { listWmsCustomer } from "@/api/wms/customer";

export default {
  components: { FormComponents, TableComponents },
  props: [],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      activeName: "1",
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
      },
      ruleForm: {},
      columns: [
        { key: 1, label: this.$t('common.customer'), visible: true },
        { key: 2, label: this.$t('fiance.total'), visible: true },
        { key: 3, label: this.$t('common.createTime'), visible: true },
        { key: 4, label: this.$t('common.operation'), visible: true },
      ],
      rules: {
        amount: [
          {
            required: true,
            message: this.$t("common.pleaseInput"),
            trigger: "blur",
          },
        ]
      },
      tabs: [{ name: this.$t('fiance.statement'), value: "" }],
      saveLoading: false,
      returnData(data) {
        return data.content.map((r) => {
          r.createTime = dayjs(r.createTime).format("YYYY-MM-DD HH:mm:ss");
          return r;
        });
      },
      columnsAry: [
        { prop: "financeNo", label: this.$t('fiance.No') },
        // { prop: "customerName", label: this.$t('common.customer') },
        { prop: "payeeName", label: this.$t('fiance.payeeName'), width: "100px" },
        { prop: "payerName", label: this.$t('fiance.payerName'), width: "100px" },
        { prop: "title", label: this.$t('common.insName') },
        { prop: "orderNo", label: this.$t('order.No') },
        // { prop: "amount", label: this.$t('fiance.total') },
        { prop: "createTime", label: this.$t('common.createTime') },
        { label: this.$t('common.operation'), slot: "operation" },
      ],
      drawer: false,
      fianceTaskId: null,
      wmsCustomerList: []
    };
  },
  computed: {
    editColumns() {
      return [
        {
          prop: "amount",
          label: this.$t("fiance.amount"),
          type: "input",
          inputType: "number",
          col: 24,
        },
      ];
    },
    YT_roles() {
      // 暂时判断roles为common不是客户的未YT
      if(this.$store.state.user.roles[0] !== 'common') {
        return true
      }
    },
  },
  created() {
    this.getList();
    this.getCusList()
  },
  methods: {
    handleClick(tab, event) {
      this.activeName = String(Number(tab.index) + 1);
      this.getList();
    },
    // 查询客户
    getCusList() {
      const query = { pageNum: undefined, pageSize: undefined };
      const pageReq = { page: 0, size: 100000 };
      listWmsCustomer(query, pageReq).then((response) => {
        const { content } = response;
        this.wmsCustomerList = content;
      });
    },
    getList() {
      this.$nextTick(() => {
        this.$refs.tableRef.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    add(index, row) {
      this.drawer = true;
      this.fianceTaskId = row.id
    },
    handleClose() {
      this.drawer = false;
      this.getList()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const api = updateBmsFinance;
          const params = {
            ...this.ruleForm,
            id: this.fianceTaskId,
            status: 'NORMAL'
          }
          console.log('params', params)
          api({ ...params }).then(res => {
            this.$message.success(this.$t("common.succcessInfo"));
            this.handleClose()
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep {
  .el-drawer__header {
    font-size: 18px;
  }
}
</style>
